package com.example.main;

import java.util.ArrayList;
import java.util.List;

public class Main11 {

    public static void main(String[] args) {
        String s="\",,,sd\",\"df\"\"d\"\",ssd\",\"d\"\",,,ss\",,sdsd'ds,sdfdf\"d,sddw";

        List<String> parse = CsvParser.instance(',').init(s).parse();
        String s1 = sqlGenerate(parse);
        System.out.println(s1);
    }

    private static String sqlGenerate(List<String> list){
        StringBuffer value = new StringBuffer();
        for (String s : list) {
            value.append("','")
                    .append(s.contains("'")?s.replace("'","''"):s)
                    .append("','");
        }
        value.delete(0,2).deleteCharAt(value.length()-1);
        value.append("sysdate");
        list.clear();
        return value.toString();
    }

    private static class CsvParser{
        private final int end=-1;
        private final char quoteChar='"';
        private final char delimiter;

        private String s;
        private int position=0;
        private int limit;

        private List<String> recordList=new ArrayList<>();
        private StringBuilder sb = new StringBuilder();

        private CsvParser(char delimiter) {
            this.delimiter = delimiter;
        }

        public static CsvParser instance(){
            return instance(',');
        }

        public static CsvParser instance(char delimiter){
            return new CsvParser(delimiter);
        }

        public List<String> parse(){
            int ch=read();
            while (!endOfString(ch)){
                if (isQuoteChar(ch)){
                    parseEncapsulated();
                }else {
                    parseSimple(ch);
                }
                ch=read();
            }
            return recordList;
        }

        private boolean endOfString(final int ch){
            return ch==end;
        }
        private boolean lastOfString(){
            return position==limit-1;
        }
        private void parseEncapsulated(){
            int ch=read();
            //while循环中不是-1
            while (!endOfString(ch)){
                if (isQuoteChar(ch)){
                    //是双引号读取下一个字符
                    ch=read();
                    //下一个字符也为双引号时解析一个双引号
                    if(isQuoteChar(ch)){
                        sb.append((char) ch);
                    }else {
                        break;
                    }
                }else {
                    //不是双引号之间加入
                    sb.append((char) ch);
                }
                //读取下一个
                ch=read();
            }
            addRecord();
        }
        private boolean isQuoteChar(final int ch) {
            return ch == quoteChar;
        }
        private boolean isDelimiter(final int ch) {
            return ch == delimiter;
        }
        private void parseSimple(int ch){
            while (!endOfString(ch)){
                if (isDelimiter(ch)){
                    break;
                }
                sb.append((char) ch);
                ch=read();
            }
            addRecord();
        }

        private int read(){
            if (position<limit){
                return s.charAt(position++);
            }
            return end;
        }

        private void addRecord(){
            recordList.add(sb.toString());
            sb=new StringBuilder();
        }

        public CsvParser init(String str){
            this.sb=new StringBuilder();
            s=str;
            this.limit=str.length();
            this.recordList=new ArrayList<>();
            this.position=0;
            return this;
        }
    }
}
